package com.jl.card.gp;

import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;
import org.mozilla.javascript.annotations.JSGetter;
import org.mozilla.javascript.annotations.JSSetter;
import org.mozilla.javascript.annotations.JSStaticFunction;

import com.jl.card.JL;
import com.jl.util.HexUtil;

public class GPKey extends ScriptableObject {
	private String ENC;
	private String MAC;
	private String DEK;

	private int keyVersion = 0x00;
	private int keyID = 0x00;

	public GPKey() {

	}

	@JSConstructor
	public GPKey(String enc, String mac, String dek) {
		this.ENC = enc;
		this.MAC = mac;
		this.DEK = dek;
	}

	@JSStaticFunction
	public static GPKey getDefault(int type) {
		switch (type) {
		case JL.GP_DOMAIN_DEFAULT:
			return new GPKey("414042434445464748494A4B4C4D4E4F",
					"414142434445464748494A4B4C4D4E4F",
					"404142434445464748494A4B4C4D4E4E");
		case JL.GP_DOMAIN_INNER:
			return new GPKey("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
					"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
					"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
		default:
			return null;
		}
	}

	@JSGetter
	public int getKeyVersion() {
		return keyVersion;
	}

	@JSSetter
	public void setKeyVersion(int keyVersion) {
		this.keyVersion = keyVersion;
	}

	@JSGetter
	public int getKeyID() {
		return keyID;
	}

	@JSSetter
	public void setKeyID(int keyID) {
		this.keyID = keyID;
	}

	public byte[] getENC() {
		return HexUtil.parseHexToByteArray(ENC);
	}

	public byte[] getMAC() {
		return HexUtil.parseHexToByteArray(MAC);
	}

	public byte[] getDEK() {
		return HexUtil.parseHexToByteArray(DEK);
	}

	@Override
	public String getClassName() {
		return "GPKey";
	}
	
	@JSFunction
	@Override
	public String toString() {
		return "[obejct GPKey]";
	}
}
